package network;

import protocol.listeners.*;
import protocol.Print;
import protocol.RoomConnection;
import protocol.User;

public class NetworkManager implements OnNewMessageListener {

	public static final NetworkManager INSTANCE = new NetworkManager();
	private OnNewMessageListener listener;
	private RoomConnection room;
	private NetworkManager() {}

	public void setListener(OnNewMessageListener listener) {
		this.listener = listener;
		if(room != null) {
			this.listener.getRoomConnection(room);
		}
	}

	@Override
	public void onCloseConnection() {
		this.room = null;
		if(listener != null)
			listener.onCloseConnection();
		Print.print("La conexion se ha cerrado");
	}

	@Override
	public int onNewMessage(String message, User user) {
		if(listener != null)
			return listener.onNewMessage(message, user);
		return 0;
	}

	@Override
	public void getRoomConnection(RoomConnection connection) {
		this.room = connection;
		if(listener != null)
			listener.getRoomConnection(room);
	}
}
